# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# from datetime import datetime

from django.db import models
from users.models import CompanyInfo

# from users.models import MiantainerInfo, PropertyPersonInfo
from utils.models import BaseModel
from users.models import Person, AuthUser
from basic.models import ElevatorInfo


class Task(BaseModel):
    """
    保养工单
    """
    TASK_STATUS_INIT = 10
    # 签到开始
    TASK_STATUS_START = 20
    # 完成
    TASK_STATUS_FINISH_ON_LINE = 30
    # 已经确认（物业确认）
    TASK_STATUS_CONFIRM = 40    # 为兼容老版本 40为流程结束

    # 完成(离线版)
    TASK_STATUS_FINISH_OFF_LINE = 50
    # 提交市局
    TASK_STATUS_TRANS = 60
    # 市局报错(不包含物业)
    TASK_STATUS_WRONG = 70
    # 市局成功(不包含物业)
    TASK_STATUS_RIGHT = 80
    # 市局报错(包含物业)
    TASK_STATUS_WRONG_CONFIRM = 90
    # 工单转移中
    TASK_STATUS_TRANS_TO_PERSON = 100
    # 首次维保报错
    FIRST_MAINTAIN_WRONG = 110
    # 维保查询报错
    NEW_MAINTAIN_WRONG = 120
    # 维保开始报错
    START_MAINTAIN_WRONG = 130
    # 维保结束报错
    END_MAINTAIN_WRONG = 140

    TASK_STATUS = (
        (TASK_STATUS_INIT, u"初始状态"),
        (TASK_STATUS_START, u"签到开始"),
        (TASK_STATUS_FINISH_ON_LINE, u"在线版完成"),
        (TASK_STATUS_CONFIRM, u"已经确认"),
        (TASK_STATUS_FINISH_OFF_LINE, u"离线版完成"),
        (TASK_STATUS_TRANS, u"提交中"),
        (TASK_STATUS_WRONG, u"市局报错"),
        (TASK_STATUS_RIGHT, u"市局成功 不包含物业"),
        (TASK_STATUS_WRONG_CONFIRM, u"市局报错(包含物业)"),
        (TASK_STATUS_TRANS_TO_PERSON, u"工单转移给维保人员"),
        (FIRST_MAINTAIN_WRONG, u"首次维保报错"),
        (NEW_MAINTAIN_WRONG, u"维保查询报错"),
        (START_MAINTAIN_WRONG, u"维保开始报错"),
        (END_MAINTAIN_WRONG, u"维保结束报错"),
    )
    # link_account = models.CharField(max_length=256, verbose_name=u"对接账号", blank=True, null=True)
    # link_token = models.CharField(max_length=256, verbose_name=u"对接令牌", blank=True, null=True)
    sn = models.CharField(max_length=128, unique=True, verbose_name=u"工单编号", db_index=True)
    day = models.DateField(verbose_name=u"维保日期", db_index=True)
    worker = models.CharField(max_length=1028, verbose_name=u"维保人员", default='[]')
    # worker_1 = models.ForeignKey(Person, verbose_name=u"维保人员1", related_name="tasks")
    # worker_2 = models.ForeignKey(Person, verbose_name=u"维保人员2", related_name="tasks_2")
    status = models.IntegerField(choices=TASK_STATUS, default=TASK_STATUS_INIT, verbose_name=u"工单状态", db_index=True)
    elevator = models.ForeignKey(ElevatorInfo, verbose_name=u"电梯", related_name="tasks", db_index=True)
    # first_maintain = models.CharField(max_length=50, verbose_name=u"首次维保日期", null=True, blank=True)
    # 维保结果和物业确认结果 用json字符串保存 灵活
    result = models.TextField(null=True, blank=True, verbose_name=u"维保结果")
    # r1 = models.CharField(max_length=4096, verbose_name=u"维保结果", null=True, blank=True)
    # r2 = models.TextField(null=True, blank=True, verbose_name=u"textfield")
    estate_confirm = models.CharField(max_length=200, verbose_name=u"物业确认", null=True, blank=True)
    desc = models.CharField(max_length=200, verbose_name=u"备注", default=u"", blank=True, null=True)
    M_T_1 = 10
    M_T_2 = 20
    M_T_3 = 30
    M_T_4 = 40
    M_T_5 = 50
    M_T_6 = 60
    M_T_7 = 70
    M_T_8 = 80
    M_T_9 = 90
    M_T_10 = 100
    M_T_11 = 110
    M_T_12 = 120
    M_T_13 = 130
    M_T_14 = 140
    M_T_15 = 150
    M_T_16 = 160
    M_T_17 = 170
    M_T_18 = 180
    M_T_19 = 190
    M_T_20 = 200
    M_T_21 = 210
    M_T_22 = 220
    M_T_23 = 230
    M_T_24 = 240
    MAINTAIN_TYPE = (
        (M_T_1, u"标准有机房年保"),
        (M_T_2, u"标准有机房季保"),
        (M_T_3, u"标准有机房半月保"),
        (M_T_4, u"标准有机房半年保"),
        (M_T_5, u"标准无机房年保"),
        (M_T_6, u"标准无机房季保"),
        (M_T_7, u"标准无机房半月保"),
        (M_T_8, u"标准无机房半年保"),
        (M_T_9, u"国标-液压电梯-年度"),
        (M_T_10, u"国标-液压电梯-季度"),
        (M_T_11, u"国标-液压电梯-半月"),
        (M_T_12, u"国标-液压电梯-半年"),
        (M_T_13, u"国标-杂物电梯-年度"),
        (M_T_14, u"国标-杂物电梯-季度"),
        (M_T_15, u"国标-杂物电梯-半月"),
        (M_T_16, u"国标-杂物电梯-半年"),
        (M_T_17, u"国标-曳引电梯-年度"),
        (M_T_18, u"国标-曳引电梯-季度"),
        (M_T_19, u"国标-曳引电梯-半月"),
        (M_T_20, u"国标-曳引电梯-半年"),
        (M_T_21, u"国标-自动扶梯-年度"),
        (M_T_22, u"国标-自动扶梯-季度"),
        (M_T_23, u"国标-自动扶梯-半月"),
        (M_T_24, u"国标-自动扶梯-半年"),
    )
    maintain_type = models.IntegerField(choices=MAINTAIN_TYPE, default=M_T_1, verbose_name=u"保养类型名称", db_index=True)
    task_start_time = models.DateTimeField(verbose_name=u"维保开始时间", db_index=True, null=True, blank=True)
    task_end_time = models.DateTimeField(verbose_name=u"维保完成时间", db_index=True, null=True, blank=True)
    property_comfirm_time = models.DateTimeField(verbose_name=u"物业确认日期", db_index=True, null=True, blank=True)
    lng = models.CharField(max_length=128, verbose_name=u"精度", null=True, blank=True)
    lat = models.CharField(max_length=128, verbose_name=u"维度", null=True, blank=True)
    signature = models.CharField(max_length=128, verbose_name=u"签名", null=True, blank=True)
    maintain_signature = models.CharField(max_length=128, verbose_name=u"维保签名", null=True, blank=True)
    maintainer_name = models.CharField(max_length=128, verbose_name=u"维保人员姓名", null=True, blank=True)
    maintainer_id = models.IntegerField(default=0, verbose_name=u"维保人员ID", db_index=True)
    flag = models.BooleanField(verbose_name=u"确定维保周期原点", default=True, db_index=True)
    is_first = models.BooleanField(verbose_name=u"是否首次维保", default=True, db_index=True)
    first_maintain_time = models.DateTimeField(verbose_name=u"首次维保时间", null=True, blank=True, db_index=True)
    REGISTRY_1 = 10
    REGISTRY_2 = 20
    REGISTRY_3 = 30
    REGISTRY = (
        (10, u'未备案'),
        (20, u'备案成功'),
        (30, u'备案失败'),
    )
    first_maintain_status = models.IntegerField(choices=REGISTRY, default=REGISTRY_1, verbose_name=u'备案状态', db_index=True)
    rwd_id = models.CharField(max_length=256, verbose_name=u"市局返回的任务单id", default=u"")
    """
    flag 是维保周期原点，一年为一个循环，第一次创建的时候是原点，年保的时候也是原点，根据离原点的时间，自动创建任务并区分类型
    is_fist 是否是首次维保 
    first_maintain_status  
    后两个字段，
    """
    position = models.CharField(max_length=125, verbose_name=u'维保人员经纬度', null=True, blank=True)
    transfer_to = models.ForeignKey(AuthUser, verbose_name=u"转移给某人", null=True, blank=True)
    need_add_time = models.BooleanField(verbose_name=u"是否需要打上时间", default=False)

    def to_json(self):
        return {
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "is_active": self.is_active,
            "update_time": self.update_time.strftime("%Y-%m-%d %H:%M:%S"),
            "update_by": self.update_by,
            "service_id": self.service_id,
            "sn": self.sn,
            "day": self.day.strftime("%Y-%m-%d"),
            "worker": self.worker,
            "status": self.status,
            "elevator_id": self.elevator_id,
            "result": self.result,
            "estate_confirm": self.estate_confirm,
            "desc": self.desc,
            "maintain_type": self.maintain_type,
            "task_end_time": self.task_end_time.strftime("%Y-%m-%d %H:%M:%S") if self.task_end_time else None,
            "property_comfirm_time": self.property_comfirm_time .strftime("%Y-%m-%d %H:%M:%S") if self.property_comfirm_time else None,
            "signature": self.signature,
            "maintain_signature": self.maintain_signature,
            "maintainer_name": self.maintainer_name,
            "maintainer_id": self.maintainer_id,
            "flag": self.flag,
            "is_first": self.is_first,
            "first_maintain_time": self.first_maintain_time.strftime("%Y-%m-%d %H:%M:%S") if self.first_maintain_time else None,
            "first_maintain_status": self.first_maintain_status,
            "rwd_id": self.rwd_id,
            "position": self.position,
            "transfer_to_id": self.transfer_to_id,
        }


class DataStorage(BaseModel):
    task = models.ForeignKey(Task, verbose_name=u"对应的工单", null=True, blank=True, db_index=True)
    # 默认是手动暂存，如果任务提交失败时，传False
    status = models.BooleanField(default=True, verbose_name=u"手动暂存", db_index=True)
    result = models.TextField(verbose_name=u"暂存的数据", null=True, blank=True, default=u'')
    position = models.CharField(max_length=125, verbose_name=u'维保人员经纬度', null=True, blank=True)


class Contract(BaseModel):
    contract_no = models.CharField(max_length=128, blank=True, null=True, verbose_name=u"合同编号", db_index=True)
    C_T_1 = 10
    C_T_2 = 20
    C_T_3 = 30
    C_T = (
        (C_T_1, u"质保"),
        (C_T_2, u"保养"),
        (C_T_3, u"维修"),
    )
    contract_type = models.IntegerField(choices=C_T, default=C_T_2, verbose_name=u"合同类型", db_index=True)
    # link_account = models.CharField(max_length=256, verbose_name=u"对接账号", blank=True, null=True)
    # link_token = models.CharField(max_length=256, verbose_name=u"对接令牌", blank=True, null=True)
    maintain_company = models.ForeignKey(CompanyInfo, verbose_name=u"维保单位", null=True, blank=True,
                                         on_delete=models.SET_NULL, related_name="maintain_company_contract", db_index=True)
    property_company = models.ForeignKey(CompanyInfo, verbose_name=u"物业单位", null=True, blank=True,
                                         on_delete=models.SET_NULL, related_name="property_company_contract", db_index=True)
    start_time = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"开始时间")
    end_time = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"结束时间")
    sign_time = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"签署时间")

    contract_name = models.CharField(max_length=256, blank=True, null=True, verbose_name=u"合同名称")

    M_C_1 = 10
    M_C_2 = 20
    M_C_3 = 30
    M_C_4 = 40
    M_C = (
        (M_C_1, u"半月保"),
        (M_C_2, u"月保"),
        (M_C_3, u"季保"),
        (M_C_4, u"年保"),
    )
    maintain_cycle = models.IntegerField(choices=M_C, default=M_C_1, verbose_name=u"维保周期", db_index=True)
    contract_file = models.CharField(max_length=4096, verbose_name=u"合同附件网络地址", blank=True, null=True)
    REGISTRY_1 = 10
    REGISTRY_2 = 20
    REGISTRY_3 = 30
    REGISTRY = (
        (10, u'未备案'),
        (20, u'备案成功'),
        (30, u'备案失败'),
    )
    registry_status = models.IntegerField(choices=REGISTRY, default=REGISTRY_1, verbose_name=u'备案状态', db_index=True)
    registry_time = models.DateTimeField(verbose_name=u'备案完成时间', null=True, blank=True)

    class Meta:
        verbose_name = u"合同信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.contract_name

